package mvp.com.bbc.newrelyframes.base;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import mvp.com.bbc.newrelyframes.R;
import mvp.com.bbc.newrelyframes.app.uitls.ActivityUtils;
import mvp.com.bbc.newrelyframes.app.uitls.StatusBarUtil;

/**
 * Created by ${cbf} on 2017/12/5.
 */

public abstract class BaseFragmentActivity extends SwipeBackActivitys {
    private List<LinearLayout> layouts = new ArrayList<>();
    protected View baseView;
    private Unbinder unbinder = null;

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        baseView = LayoutInflater.from(context).inflate(basefragmentAcLayout(), null);
        ButterKnife.bind(baseView);
        ActivityUtils.add(this);
        EventBus.getDefault().register(this);
        initView();
        initData();
        return baseView;
    }


    //父类提供一个子类给子类实现 并初始化
    protected abstract int basefragmentAcLayout();

    //父类提供一个子类给子类实现 并初始化
    protected abstract void initView();

    //父类提供一个子类给子类实现 并初始化
    protected abstract void initData();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null)
            unbinder.unbind();
        EventBus.getDefault().unregister(this);
        ActivityUtils.removeActivity(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String action) {

    }

    /**
     * 初始化标题栏
     *
     * @param title_bar 标题栏布局
     * @param title     标题名称
     * @param type      是否是图标
     * @param property  标题栏右角标个数与属性
     */
    public void initTitleBar(LinearLayout title_bar, String title, boolean type, String[] property) {
        StatusBarUtil.setNaviBarPaddingTop(this, title_bar);
        TextView title_id = (TextView) title_bar.findViewById(R.id.homepage_live_title);
        title_id.setText(title);

        LinearLayout title_bar_corner = (LinearLayout) title_bar.findViewById(R.id.title_bar_corner);
        LinearLayout break_off = (LinearLayout) title_bar.findViewById(R.id.back_off);
        break_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.break_off();
            }
        });

        if (property == null) {
            return;
        }
        if (type) {
            layouts = getCornerImg(property);
            for (int i = 0; i < layouts.size(); i++)
                title_bar_corner.addView(layouts.get(i));
        } else {
            layouts = getCornerText(property);
            for (int i = 0; i < layouts.size(); i++)
                title_bar_corner.addView(layouts.get(i));
        }
    }

    private ArrayList<LinearLayout> getCornerText(String[] property) {
        ArrayList<LinearLayout> layouts = new ArrayList<>();

        for (int i = 0; i < property.length; i++) {
            LinearLayout corner = (LinearLayout) this.getLayoutInflater().inflate(R.layout.title_bar_corner_text, null);
            TextView textView = (TextView) corner.findViewById(R.id.corner_text);
            if (property[i].equals("保存")) {

                textView.setText("保存");

            }
            layouts.add(corner);
        }

        return layouts;
    }

    public ArrayList<LinearLayout> getCornerImg(String[] property) {

        ArrayList<LinearLayout> layouts = new ArrayList<>();

        for (int i = 0; i < property.length; i++) {
            LinearLayout corner = (LinearLayout) this.getLayoutInflater().inflate(R.layout.title_bar_corner, null);
            ImageView imageView = (ImageView) corner.findViewById(R.id.corner_img);
            if (property[i].equals("搜索")) {

                imageView.setImageResource(R.mipmap.ic_launcher);

            }
            layouts.add(corner);
        }

        return layouts;
    }
}
